package com.ruoyi.project.cms.content.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.content.domain.CMSContent;
import com.ruoyi.project.cms.content.service.ICMSContentService;
import com.ruoyi.project.cms.type.domain.CMSType;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/cms/content")
public class CMSContentController extends BaseController {

    @Autowired
    private ICMSContentService icmsContentService;

    //声明资源地址的公共前缀
    private String prefix = "cms/content";

    /**
     * 进入页面
     *
     * @return
     */
    @RequiresPermissions("cms:content:view")
    @GetMapping()
    public String cmsType() {
        return prefix + "/content";
    }

    /**
     * 分页加载公告
     *
     * @param cmsContent
     * @return
     */
    @RequiresPermissions("cms:content:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CMSContent cmsContent) {
        startPage();
        List<CMSContent> list = icmsContentService.selectCmsContentList(cmsContent);
        return getDataTable(list);//返回
    }

    /**
     * 打开新增公告页面
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存公告类型
     */
    @RequiresPermissions("cms:content:add")
    @Log(title = "公告管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated CMSContent cmsContent) {
        return toAjax(icmsContentService.insertCMSContent(cmsContent));
    }

    /**
     * 删除公告类型
     */
    @RequiresPermissions("cms:content:remove")
    @Log(title = "公告管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(icmsContentService.deleteCMSContentByIds(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 打开修改公告并回显
     */
    @GetMapping("/edit/{contentId}")
    public String edit(@PathVariable("contentId") Long contentId, ModelMap mmap) {
        mmap.put("content", icmsContentService.selectCMSContentById(contentId));
        return prefix + "/edit";
    }

    /**
     * 保存修改公告
     */
    @RequiresPermissions("cms:content:edit")
    @Log(title = "公告管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated CMSContent cmsContent)
    {
        return toAjax(icmsContentService.updateCMSContent(cmsContent));
    }
}
